#ifndef HTTP_SERVER_H
#define HTTP_SERVER_H

#include "common.h"
#include "microhttpd.h"

// HTTP请求上下文
typedef struct {
    struct MHD_PostProcessor *post_processor;
    char *post_data;
    size_t post_data_size;
    json_t *json_body;
} http_request_context_t;

// HTTP响应
typedef struct {
    int status_code;
    char *content_type;
    char *body;
    size_t body_size;
} http_response_t;

// HTTP服务器
typedef struct {
    struct MHD_Daemon *daemon;
    char *host;
    int port;
    bool running;
    void *user_data;  // 用于传递系统管理器实例
} http_server_t;

// HTTP路由处理函数
typedef http_response_t *(*http_route_handler_t)(const char *method, const char *path, 
                                                json_t *query_params, json_t *body, 
                                                void *user_data);

// HTTP路由
typedef struct http_route {
    const char *method;
    const char *path_pattern;
    http_route_handler_t handler;
    struct http_route *next;
} http_route_t;

// HTTP服务器API
int http_server_init(http_server_t *server, const char *host, int port, void *user_data);
int http_server_start(http_server_t *server);
int http_server_stop(http_server_t *server);
void http_server_cleanup(http_server_t *server);

// 路由管理
int http_server_add_route(http_server_t *server, const char *method, 
                         const char *path_pattern, http_route_handler_t handler);

// 响应工具函数
http_response_t *http_response_create(int status_code, const char *content_type, const char *body);
http_response_t *http_response_json(int status_code, json_t *json_body);
http_response_t *http_response_error(int status_code, const char *error_message);
void http_response_free(http_response_t *response);

// URL编码解码
char *http_url_encode(const char *str);
char *http_url_decode(const char *str);

#endif
